﻿using CTS.Com.Domain.Enum;
using CTS.Com.Domain.Model;
using CTS.Com.Resources.Strings;

namespace CTS.Com.Domain.Helper
{
    /// <summary>
    /// MessageHelper
    /// </summary>
    public static class MessageHelper
    {
        /// <summary>
        /// Get message string from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message string</returns>
        public static string GetMessageString(string messageId, params object[] param)
        {
            // Local variable declaration
            var strMsg = string.Empty;
            // Get string external
            var rms = ContextHelper.MessageResources;
            for (int i = rms.Count - 1; i >= 0; i--) {
                var rm = rms[i];
                strMsg = rm.GetString(messageId);
                if (strMsg != null) {
                    return string.Format(strMsg, NameHelper.GetNames(param));
                }
            }
            // Get string internal
            strMsg = string.Format(Messages.ResourceManager.GetString(messageId), NameHelper.GetNames(param));
            // Return value
            return strMsg;
        }

        /// <summary>
        /// Get message from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message</returns>
        public static Message GetMessage(string messageId, params object[] param)
        {
            var message = new Message();
            message.MessageId = messageId;
            message.MessageParams = param;
            message.MessageDetail = GetMessageString(messageId, param);
            if (messageId.StartsWith("I"))
                message.MessageType = MessageType.Info;
            else if (messageId.StartsWith("W"))
                message.MessageType = MessageType.Warn;
            else if (messageId.StartsWith("E"))
                message.MessageType = MessageType.Error;
            else if (messageId.StartsWith("F"))
                message.MessageType = MessageType.Fatal;
            else message.MessageType = MessageType.None;
            return message;
        }

        /// <summary>
        /// Get message from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="messageType">Message type</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message</returns>
        public static Message GetMessage(string messageId, MessageType messageType, params object[] param)
        {
            var message = new Message();
            message.MessageId = messageId;
            message.MessageParams = param;
            message.MessageType = messageType;
            message.MessageDetail = GetMessageString(messageId, param);
            return message;
        }

        /// <summary>
        /// Get message fatal from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message</returns>
        public static Message GetMessageFatal(string messageId, params object[] param)
        {
            var message = GetMessage(messageId, MessageType.Fatal, param);
            return message;
        }

        /// <summary>
        /// Get message error from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message</returns>
        public static Message GetMessageError(string messageId, params object[] param)
        {
            return GetMessage(messageId, MessageType.Error, param);
        }

        /// <summary>
        /// Get message warn from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message</returns>
        public static Message GetMessageWarn(string messageId, params object[] param)
        {
            return GetMessage(messageId, MessageType.Warn, param);
        }

        /// <summary>
        /// Get message info from resource.
        /// </summary>
        /// <param name="messageId">Message id</param>
        /// <param name="param">Array message param</param>
        /// <returns>Message</returns>
        public static Message GetMessageInfo(string messageId, params object[] param)
        {
            return GetMessage(messageId, MessageType.Info, param);
        }
    }
}
